package redux;

import javafx.scene.control.RadioButton;

import javax.swing.*;
import java.awt.event.*;

public class MyDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTextField textField1;
    private JTextField textField2;
    private DialogCallBack mCallBack;
    private ButtonGroup mRadioGroup;
    private JRadioButton cmptRadioButton;
    private JRadioButton pageRadioButton;
    private JRadioButton screenRadioButton;
    private String mCmptName = "Page";

    public MyDialog(DialogCallBack callBack) {
        this.mCallBack = callBack;
        setTitle("Mvp Create Helper");
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        setSize(300, 280);
        setLocationRelativeTo(null);
        textField1.setText("Alex");
        buttonOK.addActionListener(e -> onOK());
        buttonCancel.addActionListener(e -> onCancel());

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });
        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        mRadioGroup = new ButtonGroup();
        mRadioGroup.add(cmptRadioButton);
        mRadioGroup.add(pageRadioButton);
        mRadioGroup.add(screenRadioButton);
        pageRadioButton.setSelected(true);
        cmptRadioButton.addActionListener(radioButtonActionListener);
        pageRadioButton.addActionListener(radioButtonActionListener);
        screenRadioButton.addActionListener(radioButtonActionListener);
    }

    private ActionListener radioButtonActionListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (pageRadioButton.isSelected()) {
                mCmptName = "Page";
            } else if (cmptRadioButton.isSelected()) {
                mCmptName = "Cmpt";
            } else if (screenRadioButton.isSelected()) {
                mCmptName = "Screen";
            }
        }
    };

    private void onOK() {
        // add your code here
        if (null != mCallBack) {
            mCallBack.ok(textField1.getText().trim(), textField2.getText().trim(), mCmptName);
        }
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

//    public static void main(String[] args) {
//        AlertDialog dialog = new AlertDialog(new DialogCallBack() {
//            @Override
//            public void ok(String author, String moduleName) {
//                System.out.print("成功啦！！！！！！ " + "author:" + author + "moduleName" + moduleName);
//            }
//        });
//        dialog.pack();
//        dialog.setVisible(true);
//        System.exit(0);
//    }

    public interface DialogCallBack {
        void ok(String author, String moduleName, String cmptName);
    }

}
